﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Main_Accounts_VoucherDisplay_Payment_view : System.Web.UI.Page
{
    double grandtotal_dr = 0;
    double grandtotal_cr = 0;
    Datautility du = new Datautility();
    DataTable DTMain = new DataTable();
    string strsql = "";
    string currentyear = "";
    string Nextyear = "";
    string currentFyear = "";
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            //**************Set Minimum and maximum date calender Control********
            AccountSettings.setMinMaxDate(form1);
            //********************End **********
            GetFinanceYear();  
        }       
    }
    protected void GetFinanceYear()
    {
        string FromDate = "01/04/" + Session["FINYEAR"].ToString();
        string ToDate = "31/03/" + (Convert.ToDouble(Session["FINYEAR"]) + 1).ToString();
        txtfrom.Text = FromDate.ToString();
        txtto.Text = ToDate.ToString();
        BindDataList(FromDate, ToDate);
    }
    protected void BindDataList(string fromdate, string todate)
    {
        if (DTMain.Rows.Count > 0)
        {
            foreach (DataRow dr in DTMain.Rows)
            {
                dr.Delete();
            }
        }   
        string[] str = fromdate.ToString().Split('/');
        string FromDateMain = str[1].ToString() + "/" + str[0].ToString() + "/" + str[2].ToString();
        string[] str1 = todate.ToString().Split('/');
        string ToDateMain = str1[1].ToString() + "/" + str1[0].ToString() + "/" + str1[2].ToString();
        SqlParameter[] parma ={
                               new SqlParameter("@TransactionTypeID","6"), // 6 for Payment
                               new SqlParameter("@FromDate",FromDateMain),
                               new SqlParameter("@todate",ToDateMain)
                              };
        DTMain = du.getdatausingprocedure("USP_ACC_GetVoucherViewDetails", parma);
        if (DTMain.Rows.Count > 0)
        {
            dtlPayment.Visible = true;
            divMessage.Visible = false;
            DataTable DT = new DataTable();
            string[] columns = { "TransactionId", "TransactionDate", "TransactionNumber","ChequeNumber","Narration" };
            DT = DTMain.DefaultView.ToTable(true, columns);
            dtlPayment.DataSource = DT;
            dtlPayment.DataBind();
        }
        else
        {
            dtlPayment.Visible = false;
            divMessage.Visible = true;
        }
    }
 
    protected void dtlPayment_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        grandtotal_dr = 0;
        grandtotal_cr = 0;
        
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Literal ltrcheque_no = (Literal)e.Item.FindControl("ltrcheque_no");
            Literal ltrnarration = (Literal)e.Item.FindControl("ltrnarration");
            Literal ltrcheque_name = (Literal)e.Item.FindControl("ltrcheque_name");
            Literal ltrnarration_name = (Literal)e.Item.FindControl("ltrnarration_name");
            HiddenField hidTranId = (HiddenField)e.Item.FindControl("hidTranId");
            GridView gridPayment = (GridView)e.Item.FindControl("gridPayment");
            HyperLink hyperEdit = (HyperLink)e.Item.FindControl("hyperEdit");
            hyperEdit.NavigateUrl = "../VoucherEntry/PaymentEdit.aspx?TranId=" + hidTranId.Value;
            DataTable DT = (DataTable)DTMain;

            SqlParameter[] paraa = { 
                                   new SqlParameter("@tranid",hidTranId.Value.ToString())
                                   };
            DataTable dtTransactionTypeID = du.getdatausingprocedure("usp_get_transactiontype", paraa);
            if (dtTransactionTypeID.Rows.Count > 0)
            {
                if (dtTransactionTypeID.Rows[0]["typeid"].ToString() != string.Empty)
                {
                    string from = "";
                    string to = "";
                    string[] d1 = txtfrom.Text.ToString().Split('/');
                    from = d1[1].ToString() + "/" + d1[0].ToString() + "/" + d1[2].ToString();
                    string[] d2 = txtto.Text.ToString().Split('/');
                    to = d2[1].ToString() + "/" + d2[0].ToString() + "/" + d2[2].ToString();


                    HyperLink hyp_print = (HyperLink)e.Item.FindControl("hyp_print");
                    hyp_print.Attributes.Add("onclick", "go('" + hidTranId.Value.ToString() + "','" + from + "','" + to + "','" + dtTransactionTypeID.Rows[0]["typeid"].ToString() + "');");
                }
            }
            if (DT.Rows.Count > 0)
            {
                DataView DView = DT.DefaultView;
                DView.RowFilter = "TransactionID='" + hidTranId.Value + "'";
                DataTable DT1 = DView.ToTable();                
                gridPayment.DataSource = DT1;
                gridPayment.DataBind();
            }          
        }
        
    }
    protected void gridPayment_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridView grdBind = (GridView)sender;
        DataListItem dlItem = (DataListItem)grdBind.Parent;
       
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Literal debit = (Literal)e.Row.Cells[1].FindControl("ltrdebit");
            Literal credit = (Literal)e.Row.Cells[1].FindControl("ltrcredit");
           
            string dk = grdBind.DataKeys[e.Row.RowIndex].Value.ToString();
            strsql = "select amount from acc_voudetails where vouserial='" + dk.ToString() + "'";
            DataTable DT = du.getdata(strsql);
            if (DT.Rows.Count > 0)
            {
                if (DT.Rows[0]["amount"].ToString() != string.Empty)
                { 
                    Literal dr = (Literal)e.Row.Cells[1].FindControl("ltrdr_cr");
                if (Convert.ToDouble(DT.Rows[0]["amount"].ToString()) > 0)
                {
                    dr.Text = "Cr";
                }
                else
                {
                    dr.Text = "Dr";
                }

                }
                
            }
            if (Convert.ToDouble(debit.Text) == 0)
            {
                debit.Text = "";
            }
            else
            {
                debit.Text = Math.Abs(Convert.ToDouble(debit.Text)).ToString("N2");
                grandtotal_dr += Convert.ToDouble(debit.Text);
            }
            if (Convert.ToDouble(credit.Text) == 0)
            {
                credit.Text = "";
            }
            else
            {
               credit.Text= Math.Abs(Convert.ToDouble(credit.Text)).ToString("N2");
               grandtotal_cr += Convert.ToDouble(credit.Text);
            }
            
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            ((Literal)e.Row.FindControl("ltrtotdr")).Text = grandtotal_dr.ToString("N2");
            ((Literal)e.Row.FindControl("ltrtotcr")).Text = grandtotal_cr.ToString("N2");
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
        }
    }
    protected void btnSearch_period_Click(object sender, EventArgs e)
    {
        BindDataList(txtfrom.Text.ToString(), txtto.Text.ToString());
    }


}
